/**
 * Created by hxpeng on 2017/6/21.
 */

(function () {
    'use strict';

    angular.module('App.pages.dynamic')
        .controller('DynamicCommentInfoCtrl', DynamicCommentInfoCtrl);

    function DynamicCommentInfoCtrl($scope,DynamicModel,$stateParams,AppUtils,$timeout,LoginAccountAuth) {

        var vm = this;
        vm.dynamic = {};
        vm.commentInfo = {};
        vm.comment = "";
        vm.dynamicId = "";
        vm.commentId = "";
        vm.hadLikesStr = "点赞+1";
        vm.toReply = null;
        vm.loginAccount = {};


        var dynamicCommentBox = $('#dynamic-comment-box1');
        var $androidMask = dynamicCommentBox.find('.weui-mask');
        $scope.$on('$ionicView.enter', function(){
            LoginAccountAuth.getAccount().then(function(data){
                vm.loginAccount = data;
            });
            vm.commentId = _.clone($stateParams.commentId);
            vm.dynamicId = _.clone($stateParams.dynamicId);
            if(!AppUtils.isUndefinedOrNull(vm.commentId)){
                DynamicModel.loadOneComment({id:vm.commentId}).$promise.then(function(response){
                    if(response.stat == 'success'){
                        vm.commentInfo = _.clone(response.dynamicComment);
                        $scope.loadChildComment();
                    }
                })
            }

            $("body").on("click","#showCommentsBtn1",function(){
                $(this).blur();
                dynamicCommentBox.fadeIn(200);
                $androidMask.on('click',function () {
                    $scope.$apply(function(){
                        vm.toReply = null;
                    });
                    dynamicCommentBox.fadeOut(200);
                });
                $timeout(function(){
                    dynamicCommentBox.find("textarea").focus();
                },200)
            })


        });
        //评论
        vm.exhortaions = [];
        vm.loadMoreBtnStatus = true;
        vm.loadMoreBtnStr = "加载更多";
        vm.pageIndex = 0;


        $scope.loadChildComment = function(){
            if(vm.loadMoreBtnStatus){
                vm.pageIndex = vm.pageIndex + 1;
                vm.loadComment();
            }
        }

        vm.loadComment = function(){
            var params = {};
            params.dynamicId = vm.dynamicId;
            params.parentCommentId = vm.commentId;
            params.pageInfo = {};
            params.pageInfo.pageSize = 10;
            params.pageInfo.pageNumber = vm.pageIndex;

            DynamicModel.loadComment(params).$promise.then(function(response){
                if(response.stat == 'err'){
                    AppUtils.showAlert(response.msg);
                }else{
                    var reponseDate = response.pageResult;
                    var items = reponseDate.pageResult;
                    var pageIndex = reponseDate.pageIndex;
                    var pageTotalPageSize = reponseDate.pageTotalPageSize;
                    if(pageIndex == pageTotalPageSize || items.length == 0){
                        vm.loadMoreBtnStatus = false;
                        vm.loadMoreBtnStr = "没有更多数据了";
                    }
                    angular.forEach(items,function(data){
                        vm.exhortaions.push(data);
                    })
                    vm.dynamic.comments = response.pageTotalItemSize;
                }
            }).finally(function() {
                $scope.$broadcast('scroll.infiniteScrollComplete');
            });
        }



        $scope.commitComment = function(){
            if(!AppUtils.isUndefinedOrNull(vm.comment)){
                var obj = {};
                obj.dynamicId = vm.dynamicId;
                obj.comment = vm.comment;
                obj.toMemberId = vm.commentInfo.memberId;
                obj.toMemberName = vm.commentInfo.memberName;
                obj.parentCommentId = vm.commentInfo.id;
                DynamicModel.commitComment(obj).$promise.then(function(response){
                    if(response.stat == 'success'){
                        AppUtils.showTipsTop("提交成功！");
                        dynamicCommentBox.fadeOut(200);
                        vm.comment = "";
                        vm.exhortaions = [];
                        vm.pageIndex = 1;
                        vm.loadComment();
                    }else{
                        AppUtils.showTipsTop("提交评论失败了！");
                    }
                })
            }else{
                AppUtils.showTipsTop("请填写评论！");
            }
        }


        $scope.deleteComment = function(commentId,index){
            if(!AppUtils.isUndefinedOrNull(commentId)){
                DynamicModel.deletedComment({id:commentId}).$promise.then(function(response){
                    if(response.stat == 'success'){
                        setTimeout(function () {
                            $scope.$apply(function(){
                                vm.exhortaions.splice(index, 1);
                            });
                        }, 10);
                    } else {
                        AppUtils.showTipsTop("操作失败，请稍后再试");
                    }
                });
            }
        }

        $scope.replyComment = function(commentId,memberId,memberName,parentCommentId,$event){
            if(!AppUtils.isUndefinedOrNull(memberId) || !AppUtils.isUndefinedOrNull(memberName)){
                vm.toReply = {};
                vm.toReply.id = memberId;
                vm.toReply.name = memberName;
                if(!AppUtils.isUndefinedOrNull(parentCommentId)){
                    vm.toReply.parentCommentId = parentCommentId;
                }else{
                    vm.toReply.parentCommentId = commentId;
                }
                $("#showCommentsBtn1").click();
            }
        }

        $scope.likeComment = function(item){
            if(item.hadLikes != '1'){
                DynamicModel.likeComment({id:item.id}).$promise.then(function(response){
                    if(response.stat === 'success'){
                        item.hadLikes = '1';
                        item.likes = Number(item.likes + 1);
                    } else {
                        AppUtils.showTipsTop("操作失败，请稍后再试");
                    }
                });
            }
        }

    }

})();